// pages/salon/salon.js
import {
  getActivityInfo,activitySubscribe,getActivitySubscribeQrCodeImg,activityRemind
} from '@/api/salon'
import {showText} from "../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    autoplay: true,
    swiperIndex: 0,
    showOrder: false, // 立即预约
    showOrderResult: false,
    salon:{},
    activityId:0,
    qr:'',
    useRemind:0,
    salon_list: [{
        img: '/gemo-image/20240828/acb2104fba9f42cda2e08a7cbce7a886.png'
      },
      {
        img: '/gemo-image/20240828/acb2104fba9f42cda2e08a7cbce7a886.png'
      },
      {
        img: '/gemo-image/20240828/acb2104fba9f42cda2e08a7cbce7a886.png'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let t = this;
    if(options.id){
      t.setData({
        activityId: options.id
      })
      this.getActivityInfoFun(options.id)
    }
  },
  getActivityInfoFun(id){
    getActivityInfo({activityId:id || this.data.activityId}).then(res=>{
      if(res.code == 200){
        var data = res.data;
        var activityImages = data.activityImages.split(",");
        data.activityImages = activityImages;
        console.log(data);
        if(!data.useRemind){
          var useRemind = 0;
        }else{
          var useRemind = 1;
        }
        this.setData({
          salon:data,
          useRemind:useRemind,
        })
      }else{
      }
    });
  },


  onShow(){
    this.setData({
      autoplay:true
    })
  },
  onHide(){
    this.setData({
      autoplay:false
    })
  },
  openOrder() {
    let t = this;
    if(t.data.salon.subscribeId){
      if(t.data.salon.subscribeId > 0){
        t.getActivitySubscribeQrCodeImg();
      }else{
        t.setData({
          showOrder: true
        });
      }
    }else{
      t.setData({
        showOrder: true
      });
    }

  },
  closeDialog(e) {
    let { type } = e.currentTarget.dataset
    this.setData({
      [type]: false
    })
  },
  getActivitySubscribeQrCodeImg(){
    let t = this;
    console.log("qr");
    //console.log({activityId:t.data.activityId});
    getActivitySubscribeQrCodeImg({activityId:t.data.activityId}).then(res=>{
      console.log(res);
      if(res.code == 200){
        t.setData({
          showOrderResult: true,
          showOrder:false,
          qr:res.data.qrCodeImg,
        })
      }
    });
  },
  confirmOrder(){
    let t = this;
    //console.log({activityId:t.data.activityId})
    activitySubscribe({activityId:t.data.activityId}).then(res=>{
      if(res.code == 200){
        t.getActivityInfoFun()
        t.getActivitySubscribeQrCodeImg();
      }else{
        showText(res.msg)
      }
    });
  },
  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  // 设置订阅消息
  setSubscribe(){
    let t = this;
    if(!t.data.useRemind){
      wx.requestSubscribeMessage({
        tmplIds: ['hhFFxZCTJPTxYaJao9aUsiuv9q91wFpnJBFuPJOHJLk'],
        success(res) {
          console.log(res)
          console.log(33333333);

          activityRemind({activityId:t.data.activityId}).then(res=>{
            if(res.code == 200){
              t.setData({
                useRemind: 1
              });
            }
          });
        },
        fail(res) {
          console.log(res)
        }
      })
    }else{
      showText("已订阅");
    }
  },
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})